import jsonschema
from api.jiuchao_login_api import JCLoginApi
from common.assert_tools import common_assert
from common.logging_use import init_log_config
from config import BASE_DIR
import logging  # 导包


class TestJCLogin(object):
    init_log_config(BASE_DIR + "/log/jc.log", interval=3, backup_count=5)  # 初始化日志

    # 定义测试方法-登录成功
    def test01_login_success(self):
        # 准备登录测试数据
        data = {"mobile": "19811796705", "password": "jxz123456!@#", "category": "0"}
        # 调用自己封装的API，获取响应结果
        resp = JCLoginApi.login(data)
        # 打印
        # print("登录成功:", resp.json())
        logging.info(f"登录成功:{resp.json()}")  # 用logging代替print,去打印到屏幕，并且写入log文件
        # 调用自己封装的断言
        # common_assert(resp, 200, 0, "")               # 使用全量字段做断言校验，代替通用断言方法，断言数据就是resp返回的，所以不用定义了
        assert 200 == resp.status_code  # 其中响应状态一般没必要放schema中断言，因为一般schema校验的是json数据
        schema = {
            "type": "object",
            "properties": {
                "code": {"const": 0},
                "data": {
                    "type": "object",
                    "properties": {
                        "userId": {"const": 29271},
                        "userType": {"const": 1},
                        "kefu": {"const": 0},
                        "category": {"const": 0},
                        "accessToken": {"type": "string"},
                        "refreshToken": {"type": "string"},
                        "expiresTime": {"type": "integer"},
                        "openid": {"type": "null"},
                        "respVO": {
                            "type": "object",
                            "properties": {
                                "id": {"const": 1660},
                                "userId": {"const": 29271},
                                "name": {"const": "测试"},
                                "businessArea": {"const": ""},
                                "status": {"const": 1},
                                "address": {"const": "测试"},
                                "type": {"const": "一般纳税人"},
                                "legalPerson": {"pattern": "测试"},
                                "picUrl": {"const": ""},
                                "businessLicenseNumber": {"const": "123456789012345678"},
                                "createTime": {"type": "integer"}
                            },
                            "required": ["id", "userId", "name", "businessArea", "status", "address", "type",
                                         "legalPerson", "picUrl", "businessLicenseNumber", "createTime"]
                        }
                    },
                    "required": ["userId"]
                },
                "msg": {"const": ""}
            },
            "required": ["code", "data", "msg"]
        }
        jsonschema.validate(instance=resp.json(), schema=schema)

    # 定义测试方法-手机号未注册
    def test02_mobile_not_register(self):
        # 准备登录测试数据
        data = {"mobile": "19811797670", "password": "jxz123456!@#", "category": "0"}
        # 调用自己封装的API，获取响应结果
        resp = JCLoginApi.login(data)
        # 打印
        print("手机号未注册:", resp.json())
        # 调用自己封装的断言
        common_assert(resp, 200, 1004001001, "手机号未注册用户")

    # 定义测试方法-密码错误
    def test03_pwd_err(self):
        # 准备登录测试数据
        data = {"mobile": "19811796705", "password": "jxz123456", "category": "0"}
        # 调用自己封装的API，获取响应结果
        resp = JCLoginApi.login(data)
        # 打印
        print("密码错误:", resp.json())
        # 调用自己封装的断言
        common_assert(resp, 200, 1004003000, "登录失败，账号密码不正确")

    # 定义方法-手机号为空
    def test05_mobile_none(self):
        data = {"mobile": None, "password": "jxz123456!@#", "category": "0"}
        resp = JCLoginApi.login(data)
        print("手机号为空", resp.json())
        common_assert(resp, 200, 400, "请求参数不正确:手机号不能为空")

    # 定义方法-手机号含有字母特殊字符
    def test06_mobile_contain_letters_characters(self):
        data = {"mobile": "19811796@A!", "password": "jxz123456!@#", "category": "0"}
        resp = JCLoginApi.login(data)
        print("手机号含有字母特殊字符", resp.json())
        common_assert(resp, 200, 400, "请求参数不正确:手机号格式不正确")

    # 定义方法-手机号10位
    def test07_mobile_ten(self):
        data = {"mobile": "1981179670", "password": "jxz123456!@#", "category": "0"}
        resp = JCLoginApi.login(data)
        print("手机号10位", resp.json())
        common_assert(resp, 200, 400, "请求参数不正确:手机号格式不正确")

    # 定义方法-手机号12位
    def test08_mobile_twelve(self):
        data = {"mobile": "198117967055", "password": "jxz123456!@#", "category": "0"}
        resp = JCLoginApi.login(data)
        print("手机号12位", resp.json())
        common_assert(resp, 200, 400, "请求参数不正确:手机号格式不正确")

    # 定义方法-密码为空
    def test09_password_none(self):
        data = {"mobile": "19811796705", "password": None, "category": "0"}
        resp = JCLoginApi.login(data)
        print("密码为空", resp.json())
        common_assert(resp, 200, 400, "请求参数不正确:密码不能为空")

    # 定义方法-密码含有字母特殊字符
    def test10_password_contain_letters_characters(self):
        data = {"mobile": "19811796705", "password": "jxz123456!@#", "category": "0"}
        resp = JCLoginApi.login(data)
        print("密码含有字母特殊字符", resp.json())
        common_assert(resp, 200, 0, "")

    # 定义方法-密码5位
    def test11_password_five(self):
        data = {"mobile": "19811796705", "password": "jxz12", "category": "0"}
        resp = JCLoginApi.login(data)
        print("密码5位", resp.json())
        common_assert(resp, 200, 400, "密码长度为 6-15 位")

    # 定义方法-密码16位
    def test12_password_sixteen(self):
        data = {"mobile": "19811796705", "password": "jxz1234567890123", "category": "0"}
        resp = JCLoginApi.login(data)
        print("密码16位", resp.json())
        common_assert(resp, 200, 400, "密码长度为 6-15 位")

    # 定义方法-多参abc
    def test13_more_parameter_abc(self):
        data = {"mobile": "19811796705", "password": "jxz123456!@#", "category": "0", "abc": "123"}
        resp = JCLoginApi.login(data)
        print("多参abc", resp.json())
        common_assert(resp, 200, 0, "")

    # 定义方法-少参mobile
    def test14_less_parameter_mobile(self):
        data = {"password": "jxz123456!@#", "category": "0"}
        resp = JCLoginApi.login(data)
        print("少参mobile", resp.json())
        common_assert(resp, 200, 400, "请求参数不正确:手机号不能为空")

    # # 定义方法-无参
    # def test15_no_parameter(self):
    #     data = None
    #     resp = JCLoginApi.login(data)
    #     print("无参", resp.json())
    #
    # # 定义方法-修改password为abc
    # def test16_password_update_abc(self):
    #     data = {"mobile": "19811796705", "abc": "jxz123456", "category": "0"}
    #     resp = JCLoginApi.login(data)
    #     print("错误参数", resp.json())
    # 因为无参和错误参数这两个方法的断言有可能是手机号不能为空有可能是密码错误，所以先不要了，因为断言没法弄
